//
// Created by shen on 2024/12/18.
//

#include "JavaCallHelper.h"
JavaCallHelper::JavaCallHelper(JavaVM *_javaVM, JNIEnv *_env, jobject &_jobj):javaVm(_javaVM),env(_env) {
    jobj = env->NewGlobalRef(_jobj);
    jclass jclass1 = env->GetObjectClass(jobj);
    jmid_prepera = env->GetMethodID(jclass1,"onPrepare","()V");
}

JavaCallHelper::~JavaCallHelper() {
    jobj=0;
}

void JavaCallHelper::onLoadMessage(int code,int thread) {

    if (thread==THREAD_CHILD){
        JNIEnv *jniEnv;
        if(javaVm->AttachCurrentThread(&jniEnv,0) != JNI_OK){
            return;
        }
        jniEnv->CallVoidMethod(jobj,jmid_prepera,code);
        javaVm->DetachCurrentThread();
    }else{
        env->CallVoidMethod(jobj,jmid_prepera);
    }

}
void JavaCallHelper::onPrepare(int thread) {
    if (thread==THREAD_CHILD){
        JNIEnv *jniEnv;
        if(javaVm->AttachCurrentThread(&jniEnv,0) != JNI_OK){
            return;
        }
        jniEnv->CallVoidMethod(jobj,jmid_prepera);
        javaVm->DetachCurrentThread();
    }else{
        env->CallVoidMethod(jobj,jmid_prepera);
    }



}